package com.onstar.cn.common.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Edward
 */
public class StringUntil {

    public static String replaceAll(String str,String oldStr,String newStr){
        int index = 0;
        int startIndex = str.indexOf(oldStr);
        StringBuffer sb = new StringBuffer();
        
        while(startIndex < str.length() && startIndex != -1)   {       
            String temp = str.substring(index,startIndex);       
            index = startIndex + oldStr.length();
            sb.append(temp);
            sb.append(newStr);
            startIndex = str.indexOf(oldStr,index);                      
        }
        sb.append(str.substring(index));
        return sb.toString();
    }
    
    public static String[] split(String original,String regex){
        int startIndex = 0;  
        Vector<String> v = new Vector<String>();      
        String[] str = null;            
        int index = 0;      
        startIndex = original.indexOf(regex);       
        while(startIndex < original.length() && startIndex != -1)   {       
            String temp = original.substring(index,startIndex);       
            v.addElement(temp);       
            index = startIndex + regex.length();       
            startIndex = original.indexOf(regex,startIndex + regex.length());                      
        }         
        v.addElement(original.substring(index + 1 - regex.length()));      
        str = new String[v.size()];      
        for(int i=0;i<v.size();i++)         
        str[i] = v.elementAt(i);
        return str; 
    }
    
    public static String format(String original,String[] args){
        if(original.length() == 0 || original.indexOf("{0}") == -1)return original;
        for(int i=0;i<args.length;i++){
            int startIndex = original.indexOf("{"+i+"}");
            String leftPart = original.substring(0,startIndex);
            int index = startIndex + ("{"+i+"}").length();
            String rigthPart = original.substring(index);
            original = leftPart+args[i]+rigthPart;
        }
        return original;
    }
    
    public static byte[] stringToBytes(String Str) { 
        byte[] STB = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeUTF(Str);
            STB = baos.toByteArray();
            baos.close();
            dos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return STB;
    }
    
    public static String bytestoString(byte[] rec) { 
        ByteArrayInputStream bais = new ByteArrayInputStream(rec);
        DataInputStream dis = new DataInputStream(bais);
        String BTS=null;
        try {
            BTS=dis.readUTF();
            bais.close();
            dis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  BTS;
    }
    
    public static String replace(String s, String f, String r) {
        if (s == null)
            return s;
        if (f == null)
            return s;
        if (r == null)
            r = "";

        int index01 = s.indexOf(f);
        while (index01 != -1) {
            s = s.substring(0, index01) + r + s.substring(index01 + f.length());
            index01 += r.length();
            index01 = s.indexOf(f, index01);
        }
        return s;
    }
    
    public static String removeHtml(String text) {
        try {
            int idx = text.indexOf("<");
            if (idx == -1)
                return text;

            String plainText = "";
            String htmlText = text;
            int htmlStartIndex = htmlText.indexOf("<", 0);
            if (htmlStartIndex == -1) {
                return text;
            }
            while (htmlStartIndex >= 0) {
                plainText += htmlText.substring(0, htmlStartIndex);
                int htmlEndIndex = htmlText.indexOf(">", htmlStartIndex);
                htmlText = htmlText.substring(htmlEndIndex + 1);
                htmlStartIndex = htmlText.indexOf("<", 0);
            }
            plainText = plainText.trim();
            return plainText;
        } catch (Exception e) {
            System.err.println("Error while removing HTML: " + e.toString());
            return text;
        }
    }

    /**
     * @description 将数字进行前缀0补位对齐
     * @author kai zhang
     * @create 2015.12.10
     * @param number 原数字
     * @param digit 位数
     * @return
     */
    public static String prefixZero(int number, int digit) {
        String numberStr = number + "";
        if (digit > numberStr.length()) {
            for (int i = 0; i < digit - numberStr.length(); i++) {
                numberStr = "0" + numberStr;
            }
        }
        return numberStr;
    }
}
